package excercise;

public class EX07_Q32 {

	// 주민번호 유효성 검사
	// 123456-1234567
	// 1*2 + 2*3 + 3*4 + 4*5 + 5*6 + 6*7
	// 1*8 + 2*9 + 3*2 + 4*3 + 5*4 + 6*5
	
	public static void main(String[] args) {
		long jumin = 1234561234567L;
		int key = 0;			// 추출된 각 자릿수 값
		int total = 0;
		
		// 123456
		// 123456 / 100000 => 1
		// 123456 % (1*100000) => 23456
		
		// 23456 / 10000 => 2
		// 23456 % (2*10000) => 3456
		
		// 3456 / 1000 => 3
		// 3456 % (3 * 1000) => 456
		
		// 첫째자리수
		key = (int)(jumin / 1000000000000L);
		total = total + (key * 2);
		jumin = jumin % (key * 1000000000000L);

		// 둘째자리수		
		key = (int)(jumin / 100000000000L);
		total = total + (key * 3);
		if (key > 0) jumin = jumin % (key * 100000000000L);
		
		// 셋째자리수
		key = (int)(jumin / 10000000000L);
		total = total + (key * 4);
		if (key > 0) jumin = jumin % (key * 10000000000L);

		// 네째자리수
		key = (int)(jumin / 1000000000L);
		total = total + (key * 5);
		if (key > 0) jumin = jumin % (key * 1000000000L);
		
		// 다섯째 자리
		key = (int)(jumin / 100000000L);
		total = total + (key * 6);
		if (key > 0) jumin = jumin % (key * 100000000L);
		
		// 여섯째 자리
		key = (int)(jumin / 10000000L);
		total = total + (key * 7);
		if (key > 0) jumin = jumin % (key * 10000000L);
		
		// 일곱째 자리
		key = (int)(jumin / 1000000L);
		total = total + (key * 8);
		if (key > 0) jumin = jumin % (key * 1000000L);
		
		// 여덟째 자리
		key = (int)(jumin / 100000L);
		total = total + (key * 9);
		if (key > 0) jumin = jumin % (key * 100000L);
		
		// 아홉째 자리
		key = (int)(jumin / 10000L);
		total = total + (key * 2);
		if (key > 0) jumin = jumin % (key * 10000L);
		
		// 열째 자리
		key = (int)(jumin / 1000L);
		total = total + (key * 3);
		if (key > 0) jumin = jumin % (key * 1000L);
		
		// 열한번째 자리
		key = (int)(jumin / 100L);
		total = total + (key * 4);
		if (key > 0) jumin = jumin % (key * 100L);
		
		// 열두번째 자리
		key = (int)(jumin / 10L);
		total = total + (key * 5);
		if (key > 0) jumin = jumin % (key * 10L);
		
		System.out.println( jumin );
		System.out.println( total );
		System.out.println( (11 - total % 11) % 10 );
		
		// 반복문으로 처리
		//System.out.println( Math.pow(10, 12) );
		jumin = 1234561234567L;
		total = 0;
		//for( long divider = 1000000000000L, prime = 2; 
		//	   divider >= 10; divider /= 10, ++prime ) {
		for( int pow = 12, prime = 2; pow >= 1; --pow, ++prime ) {
			long divider = (long) Math.pow(10, pow);
			if (prime > 9) prime = 2;
			
			key = (int)(jumin / divider );
			total = total + (key * (int)prime);
			if (key > 0) jumin = jumin % (key * divider );
		}
		System.out.println( jumin );
		System.out.println( total );
		System.out.println( (11 - total % 11) % 10 );
		
		
	}

}







